<?php
/**
 * Nadëb (Makú-Nadëb)
 * 
 * @author     Mateus Martins <mateusweb@gmail.com>
 * @copyright  Copyright 2010 mateusmartins.com.br
 * @license    http://www.gnu.org/licenses/gpl.html | GPL
 * @package    CropImages
 * @version    2.0.0
 */


/**
 * Class CropImages
 * Redimenciona imagens
 * 
 * @category   Nadeb Images
 * @package    CropImages
 * @copyright  Copyright 2012 mateusmartins.com.br
 * @license    http://www.gnu.org/licenses/gpl.html | GPL
 */
class CropImages
{
	/**
	 * @var string
	 */
	private $newFile;
	private $file;
	private $img;
	private $patch;
	private $newPatch;
	private $fileName;
	private $newName;
	private $quality;
	
	/**
	 * @var int
	 */
	private $width;
	private $height;
	private $newWidth;
	private $newHeight;
	private $finalWidth;
	private $finalHeight;
	
	/**
	 * @var array
	 */
	private $rgbBackground;
	
	/**
	 * Metodo construtor
	 * 
	 * @return void
	 */
	public function __construct()
	{
	}
	
    /**
     * Seta a cor de fundo da imagem
     *
     * @return CropImages
     */
	public function setRGB(array $value)
	{
		$this->rgbBackground = $value;
		
		return $this;
	}
	
    /**
     * Seta o caminho da imagem 
     *
     * @return CropImages
     */
	public function setPatch($patch)
	{
		$this->patch = $patch;
		
		return $this;
	}
	
    /**
     * Seta o novo caminho da imagem 
     *
     * @return CropImages
     */
	public function setNewPatch($patch)
	{
		$this->newPatch = $patch;
		
		return $this;
	}
	
    /**
     * Seta o nome da imagem 
     *
     * @return CropImages
     */
	public function setFileName($name)
	{
		$this->fileName = $name;
		
		return $this;
	}
	
    /**
     * Seta o novo nome da imagem 
     *
     * @return CropImages
     */
	public function setNewName($name)
	{
		$this->newName = $name;
		
		return $this;
	}
	
    /**
     * Seta o novo tamanho da imagem 
     *
     * @return CropImages
     */
	public function setNewSize($h = null, $w = null)
	{
		$this->newWidth  = $w;
		$this->newHeight = $h;
		
		return $this;
	}
	
    /**
     * Seta a qualidade do arquivo gerado
     *
     * @return CropImages
     */
	public function setQuality($quality)
	{
		$this->quality = $quality;
		
		return $this;
	}

	/**
	 * Auto Crop para a imagem 
	 * 
	 *  @return Nadeb_CropImages
	 */
	public function cropFile()
	{
		$this->makeFile();
		
		$ratioWidth = ceil( $this->width / $this->height * $this->newHeight );
		$ratioHeight = ceil( $this->height / $this->width * $this->newWidth );
		
		if( ($this->newWidth * $ratioHeight) > ($ratioWidth * $this->newHeight) )
		{
			$this->finalWidth = $this->newWidth;
			$this->finalHeight = $ratioHeight;
		}
		
		if( ($this->newWidth * $ratioHeight) < ($ratioWidth * $this->newHeight) )
		{
			$this->finalWidth = $ratioWidth;
			$this->finalHeight = $this->newHeight;
		}
		
		return $this;
	}

	/**
	 * Redimenciona a imagem 
	 * 
	 *  @return Nadeb_CropImages
	 */
	public function resizeFile()
	{
		$this->makeFile();
		
		$ratioWidth = ceil( $this->width / $this->height * $this->newHeight );
		$ratioHeight = ceil( $this->height / $this->width * $this->newWidth );
		
		if( ($this->newWidth * $ratioHeight) < ($ratioWidth * $this->newHeight) )
		{
			$this->finalWidth = $this->newWidth;
			$this->finalHeight = $ratioHeight;
		}
		
		if( ($this->newWidth * $ratioHeight) > ($ratioWidth * $this->newHeight) )
		{
			$this->finalWidth = $ratioWidth;
			$this->finalHeight = $this->newHeight;
		}
		
		return $this;
	}
	
	/**
	 * Cria uma instancia de imagem
	 * Usa o metodo makeFile para criar e salvar a imagem no disco
	 * 
	 *  @return CropImages
	 */
	private function makeFile()
	{
	    $this->file = $this->patch . '/' . $this->fileName;
	    $this->newFile = $this->newPatch . '/' . $this->newName;
	    
	    if( !is_file($this->file) ) throw new Exception('Image Not Found');
	    
    	if( stripos( $this->file,"gif" ) ) $this->img = @imagecreatefromgif( $this->file );
		
		if( stripos( $this->file,"png" ) ) $this->img = @imagecreatefrompng( $this->file );
		
		if( stripos( $this->file,"jpg" ) ) $this->img = @imagecreatefromjpeg( $this->file );
		
		if( stripos( $this->file,"jpeg" ) ) $this->img = @imagecreatefromjpeg( $this->file );

		$this->width  = @imagesx( $this->img );
		$this->height = @imagesy( $this->img );
	}
	
	/**
	* Salvar a imagem no disco
	*
	*  @return void
	*/
	public function create()
	{
		$this->rgbBackground = $this->rgbBackground ? $this->rgbBackground : array( 0, 0, 0 );
		$this->quality = $this->quality ? $this->quality : 75;
		$this->xPosit = - ($this->finalWidth - $this->newWidth) / 2;
		$this->yPosit = - ($this->finalHeight - $this->newHeight) / 2;
		
		$tmpImg = @imagecreatetruecolor( $this->newWidth, $this->newHeight );
		$color = @imagecolorallocate($tmpImg, $this->rgbBackground[0], $this->rgbBackground[1], $this->rgbBackground[2]);
		@imagefilledrectangle($tmpImg, 0, 0, $this->newWidth, $this->newHeight, $color);
		
		@imagecopyresampled(
			$tmpImg,
			$this->img,
			$this->xPosit, $this->yPosit, 0, 0,
			$this->finalWidth,
			$this->finalHeight,
			$this->width,
			$this->height
		);
	
		if( stripos( $this->file,"gif" ) ) @imagegif( $tmpImg, $this->newFile, $this->quality );
		if( stripos( $this->file,"png" ) ) @imagepng( $tmpImg, $this->newFile, $this->quality );
		if( stripos( $this->file,"jpg" ) ) @imagejpeg( $tmpImg, $this->newFile, $this->quality );
		if( stripos( $this->file,"jpeg" ) ) @imagejpeg( $tmpImg, $this->newFile, $this->quality );
		
		imagedestroy($tmpImg);
	}
}